package com.cw.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.cw.auth.application.convert.AuthRoleDTOConverter;
import com.cw.auth.application.convert.AuthUserDTOConverter;
import com.cw.auth.application.dto.AuthRoleDTO;
import com.cw.auth.application.dto.AuthUserDTO;
import com.cw.auth.common.entity.Result;
import com.cw.auth.domain.entity.AuthRoleBO;
import com.cw.auth.domain.entity.AuthUserBO;
import com.cw.auth.domain.service.AuthRoleDomainService;
import com.cw.auth.infra.basic.service.AuthRoleService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Description:
 *
 * @Author:
 * @Date: 2024/4/28 19:06
 */

@RestController
@Slf4j
@RequestMapping("/role")
public class RoleController {
    @Resource
    private AuthRoleDomainService authRoleDomainService;

/**
 * @author: 江传文
 * @date: 2024/4/28 20:38
 *
 * 新增角色
 */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.add.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            // 检查传入的字符串是否为 null、空字符串或者只包含空白字符（空格、制表符、换行符等）
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色名不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()), "角色标识不能为空");
            // (DTO -> BO)
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE
                    .convertDtoToBO(authRoleDTO);

            return Result.ok(authRoleDomainService.add(authRoleBO));
        } catch (Exception e) {
            log.error("UserController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增角色失败");
        }
    }

    /**
     * @author: 江传文
     * @date: 2024/4/28 20:38
     *
     * 更新角色
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            // 检查传入的字符串是否为 null、空字符串或者只包含空白字符（空格、制表符、换行符等）
            Preconditions.checkNotNull(authRoleDTO.getId(), "id不能为空");
            // (DTO -> BO)
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE
                    .convertDtoToBO(authRoleDTO);

            return Result.ok(authRoleDomainService.update(authRoleBO));
        } catch (Exception e) {
            log.error("UserController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新角色失败");
        }
    }

    /**
     * @author: 江传文
     * @date: 2024/4/28 20:38
     *
     * 删除角色
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.delete.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            // 检查传入的字符串是否为 null、空字符串或者只包含空白字符（空格、制表符、换行符等）
            Preconditions.checkNotNull(authRoleDTO.getId(), "id不能为空");
            // (DTO -> BO)
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE
                    .convertDtoToBO(authRoleDTO);

            return Result.ok(authRoleDomainService.delete(authRoleBO));
        } catch (Exception e) {
            log.error("UserController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除角色失败");
        }
    }
}
